package com.zwr.sheller.shop.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.pojo.Merchant;
import com.zwr.sheller.core.pojo.Seller;
import com.zwr.sheller.core.pojo.Store;
import com.zwr.sheller.shop.service.SellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author LSY
 * @Date 2022/1/7 9:26
 * @Version 1.0
 **/
@RestController
@RequestMapping("/seller")
@Api(tags = "商户管理员接口集")
@CrossOrigin
public class SellerController {

    SellerService sellerService;
    @Autowired
    public SellerController(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @PostMapping(value = "/findSellers")
    @ApiOperation(value = "查找该商铺对应的所有管理员的信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "num", value = "每页条数", required = true, dataType = "Integer")
    })
    public Map<String, Object> findSellers(Integer page, Integer num) {
        Map<String, Object> sellers = sellerService.findSellers(page, num);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setAll(sellers);

        return jsonResult.getMap();
    }

    @PostMapping(value = "/ShellerRegister")
    @ApiOperation(value = "商户管理员注册，同时绑定商户和商家信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "手机校验码", required = true, paramType = "query", dataType = "String"),

            @ApiImplicitParam(name = "businessLicenseStatus", value = "是否传了营业许可证图片", required = true, paramType = "query", dataType = "Boolean"),
            @ApiImplicitParam(name = "tradeLicenseStatus", value = "是否传了营业执照图片", required = true, paramType = "query", dataType = "Boolean"),
            @ApiImplicitParam(name = "idCardFrontStatus", value = "是否传了商户负责人身份证正面图片", required = true, paramType = "query", dataType = "Boolean"),
            @ApiImplicitParam(name = "idCardBackStatus", value = "是否传了商户负责人身份证背面图片", required = true, paramType = "query", dataType = "Boolean"),
            @ApiImplicitParam(name = "storeLogoStatus", value = "是否传了店铺logo图片", required = true, paramType = "query", dataType = "Boolean"),
            @ApiImplicitParam(name = "storeBackgroundStatus", value = "是否传了店铺背景图片", required = true, paramType = "query", dataType = "Boolean"),

            @ApiImplicitParam(name = "sellerPhone", value = "卖家端管理员电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sellerPwd", value = "卖家端管理员密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sellerMail", value = "卖家端管理员邮箱", dataType = "String"),

            @ApiImplicitParam(name = "merchantName", value = "商户负责人名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "merchantPhone", value = "商户负责人手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "merchantMail", value = "商户负责人邮箱", dataType = "String"),
            @ApiImplicitParam(name = "merchantAddress", value = "商户地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "businessLicense", value = "营业许可证图片路径", dataType = "String"),
            @ApiImplicitParam(name = "tradeLicense", value = "营业执照图片路径", dataType = "String"),
            @ApiImplicitParam(name = "idCardFront", value = "商户负责人身份证正面图片路径", dataType = "String"),
            @ApiImplicitParam(name = "idCardBack", value = "商户负责人身份证背面图片路径", dataType = "String"),

            @ApiImplicitParam(name = "storeName", value = "店铺名称", dataType = "String"),
            @ApiImplicitParam(name = "storeKeyName", value = "店铺关键字", dataType = "String"),
            @ApiImplicitParam(name = "storePhone", value = "店铺客服电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "storeAddress", value = "店铺网址", dataType = "String"),
            @ApiImplicitParam(name = "storeLogo", value = "店铺logo地址", dataType = "String"),
            @ApiImplicitParam(name = "storeTopic", value = "店铺公告", dataType = "String"),
            @ApiImplicitParam(name = "storeDesc", value = "店铺描述", dataType = "String"),
            @ApiImplicitParam(name = "storeBackground", value = "店铺背景链接", dataType = "String"),
            @ApiImplicitParam(name = "storeSetType", value = "店铺重复设置：0.不重复，1.左右平铺，2.垂直平铺", dataType = "Long"),
            @ApiImplicitParam(name = "storeBackgroundColor", value = "店铺背景颜色，存放RGB值", dataType = "String"),
            @ApiImplicitParam(name = "storeBackgroundOrColor", value = "店铺背景是否启用：0.启用背景，1.启用颜色", dataType = "Boolean"),
            @ApiImplicitParam(name = "storeSwitch", value = "店铺颜色或背景是否启用开关", dataType = "Boolean"),
            @ApiImplicitParam(name = "store12hour", value = "是否12小时送达：0.未开启，1.已开启", dataType = "Boolean"),
            @ApiImplicitParam(name = "storeQuality", value = "是否质量保证：0.未开启，1.已开启", dataType = "Boolean"),
            @ApiImplicitParam(name = "storeAddNew", value = "是否破损补寄：0.未开启，1.已开启", dataType = "Boolean")
    })
    public Map<String, Object> ShellerRegister(Seller seller, Merchant merchant, Store store, String code, Boolean businessLicenseStatus, Boolean tradeLicenseStatus, Boolean idCardFrontStatus, Boolean idCardBackStatus, Boolean storeLogoStatus, Boolean storeBackgroundStatus) throws LSYBusinessException {
        sellerService.ShellerRegister(seller, merchant, store, code, businessLicenseStatus, tradeLicenseStatus, idCardFrontStatus, idCardBackStatus, storeLogoStatus, storeBackgroundStatus);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("注册成功，欢迎入驻");

        return jsonResult.getMap();
    }

    /**
     * 需要验证手机号不能重复
     * 用ThreadLocal找到该用户对应的店铺的id，将新添加的管理员设置为该店铺的管理员
     *
     * @param seller
     * @return
     */
    @PostMapping(value = "/addSeller")
    @ApiOperation(value = "添加一个该商户的管理员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sellerPhone", value = "卖家端管理员电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sellerPwd", value = "卖家端管理员密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sellerMail", value = "卖家端管理员邮箱", dataType = "String")
    })
    public Map<String, Object> addSeller(Seller seller) throws LSYBusinessException {
        sellerService.addSeller(seller);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");

        return jsonResult.getMap();
    }

    /**
     * 只接收账户和邮箱保证只能修改这两个而不能修改其他的
     *
     * @param sid
     * @param account
     * @param mail
     * @return
     */
    @PostMapping(value = "/updateSeller")
    @ApiOperation(value = "修改一个该商户对应的管理员信息（需要传递id）（可以不是本账号，只能修改账号和邮箱！）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "需要修改的管理员的id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "account", value = "需要修改的管理员的账号", dataType = "String"),
            @ApiImplicitParam(name = "mail", value = "需要修改的管理员的邮箱", dataType = "String")
    })
    public Map<String, Object> updateSeller(Long sid, String account, String mail) {
        sellerService.updateSeller(sid, account, mail);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }

    /**
     * 需要存到Redis中，键是手机号
     *
     * @param phone
     * @return
     */
    @PostMapping(value = "/sendSMSVerificationCode")
    @ApiOperation(value = "发送短信验证码接口")
    @ApiImplicitParam(name = "phone", value = "需要接收验证码的的管理员手机号", dataType = "String", required = true)
    public Map<String, Object> sendSMSVerificationCode(String phone) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("code", sellerService.sendSMSVerificationCode(phone));

        return jsonResult.getMap();
    }

    @PostMapping(value = "/updateSellerPassword")
    @ApiOperation(value = "修改当前管理员的密码，需要先请求短信验证码接口，将校验码一起传递过来")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "需要修改的管理员密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", required = true)
    })
    public Map<String, Object> updateSellerPassword(String password, String code) throws LSYBusinessException {
        sellerService.updateSellerPassword(password, code);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }

    /**
     * 修改手机号时需要验证手机号是否在数据库中重复
     *
     * @param phone
     * @param code
     * @return
     */
    @PostMapping(value = "/updateSellerPhone")
    @ApiOperation(value = "修改当前管理员的手机号，需要先请求短信验证码接口，将校验码一起传递过来")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "需要修改的管理员手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", required = true)
    })
    public Map<String, Object> updateSellerPhone(String phone, String code) throws LSYBusinessException {
        sellerService.updateSellerPhone(phone, code);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }
}
